package bibliotecas;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JTextLabel extends JPanel{

	private static final long serialVersionUID = 5723618247363371212L;
	
	public JLabel label = new JLabel();
	public JTextField texto = new JTextField();
	private int posicaoLabel = 1;
	public static final int LABEL_ACIMA = 0;
	public static final int LABEL_ESQUERDA = 1;
	private int espacoEntreLabelETexto = 10;
	
	public JTextLabel(String textoLabel,String textoConteudo){
		this.setLayout(null);
		
		label.setText(textoLabel);
		texto.setText(textoConteudo);
		
		init();
	}
	
	public void init(){
		texto.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent arg0) {
				texto.setBackground(Color.WHITE);
				texto.setForeground(Color.BLACK);
			}			
			@Override
			public void focusGained(FocusEvent arg0) {
				texto.setBackground(Color.LIGHT_GRAY);
				texto.setForeground(Color.BLACK);
				texto.selectAll();
			}
		});
		texto.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e) {
				texto.nextFocus();
			}
		});
	}
	
	public void setEspacoEntreLabelETexto(int x){
		espacoEntreLabelETexto = x;
		setDimensoes(getX(),getY(),getWidth(),getHeight());
	}
	
	public void setHabilitado(boolean habilitado){
		texto.setEditable(habilitado);
		texto.setBackground(Color.WHITE);
		texto.setFocusable(false);
	}
	
	public void setTexto(String texto){
		this.texto.setText(texto);
	}
	
	public String getTexto(){
		return this.texto.getText();
	}
	
	public void setPosicaoLabel(int posicao){
		this.posicaoLabel = posicao;
		setDimensoes(getX(),getY(),getWidth(),getHeight());
	}
	
	public void setDimensoes(int x,int y,int width,int height){
		init();
		int tamanhoLabel = (label.getText().length() * label.getFont().getSize() - 2);
		
		if(posicaoLabel == JTextLabel.LABEL_ACIMA){
			setBounds(x,y,width,height);
			label.setBounds(4,0,tamanhoLabel,label.getFont().getSize());
			texto.setBounds(4,label.getFont().getSize() + espacoEntreLabelETexto,width - 8,height - label.getFont().getSize() - espacoEntreLabelETexto - 4);
		}else
		if(posicaoLabel == JTextLabel.LABEL_ESQUERDA){
			setBounds(x,y,width,height);
			label.setBounds(4,0,tamanhoLabel - espacoEntreLabelETexto,height);
			texto.setBounds(tamanhoLabel + espacoEntreLabelETexto,2,width - tamanhoLabel - espacoEntreLabelETexto - 2,height - 4);			
		}
		add(label);
		add(texto);
	}

}
